package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.FreebackDao;
import com.gdit.zax.entitys.Freeback;
import com.gdit.zax.entitys.StartPic;
import com.gdit.zax.utils.DBUtil;

public class FreebackDaoImpl implements FreebackDao{
	private PreparedStatement ps;
	private ResultSet rs;
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public FreebackDaoImpl(Connection conn,HttpServletRequest request, HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}
	
	public boolean addFreebak(Freeback fb) {
		String sql = "insert t_freeback set NickName=?,Cantact=?,Content=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, fb.getNickname());
			ps.setString(2, fb.getCantact());
			ps.setString(3,fb.getContent());
			
			int result = ps.executeUpdate();
			if (result!=0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean delFreebak(int fid) {
		String sql = "delete from t_freeback where id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, fid);
			
			int result = ps.executeUpdate();
			if (result!=0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public ArrayList<Freeback> getFreeback(int page, int onepagecount) {
		ArrayList<Freeback> list = new ArrayList<Freeback>();
		try {
			String sql = "select * from t_freeback group by Id desc limit ?,?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (page-1)*onepagecount);
			ps.setInt(2, onepagecount);
			
			rs = ps.executeQuery();
			while(rs.next()){
				list.add(DBUtil.getobject(rs, Freeback.class));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
